/* ************************************************************************************* 
 * CREATED BY:          MAI THANH HUYEN
 * CREATED DATE:        27/08/2008
 * PROJECT:             MCCMS.BACKEND PROJECT
 * PURPOSE:             
 * MODIFIED BY:         None
 * MODIFIED DATE:       None
 * MODIFIED REASON:     None
 * NOTES:               None
 **************************************************************************************/
using System;
using System.Collections.Generic;
using System.Xml;
using HtmlAgilityPack;
using System.Web.UI;
using System.Collections;


namespace MCWEBSITE.BUL
{
    public class ConvertHelper
    {
        #region "Convert Image Src String"
        /// <summary>
        /// Convert Image src string
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="strTime"></param>
        /// <param name="NewsID"></param>
        /// <returns></returns>
        public static string ConvertImgString(string ApplicationName, string strContent, string strTime, int NewsID)
        {
            string strold = "";
            string strnew = "";
            string strtemp = "";
            strTime = strTime.Replace('/', '_');
            strTime = strTime.Trim();
            string[] arrImage = GetImageScr(strContent);
            string[] arrDoc = GetDocScr(strContent);
            if (arrImage != null)
            {
                for (int i = 0; i < arrImage.Length; i++)
                {
                    if (arrImage[i] != "")
                    {
                        strold = "src=\"" + arrImage[i].ToString() + "\"";
                        if (MCWC.Utilities.HTMLUtil.Left(arrImage[i], 1) != "#")
                        {
                            if (arrImage[i].Contains("/"))
                            {
                                strtemp = arrImage[i].Substring(arrImage[i].LastIndexOf('/') + 1);
                                strnew = "src=\"/" + ApplicationName + "/Resources/NewsImages/" + strTime + "/" + NewsID.ToString() + "/" + strtemp.Trim() + "\""; ;
                                strContent = strContent.Replace(strold, strnew);
                            }
                            else
                            {
                                strnew = "src=\"/" + ApplicationName + "/Resources/NewsImages/" + strTime + "/" + NewsID.ToString() + "/" + arrImage[i].ToString() + "\"";
                                strContent = strContent.Replace(strold, strnew);
                            }
                        }
                    }
                }
            }
            if (arrDoc != null)
            {
                for (int i = 0; i < arrDoc.Length; i++)
                {
                    if (arrDoc[i] != "")
                    {
                        strold = "href=\"" + arrDoc[i].ToString() + "\"";
                        if (MCWC.Utilities.HTMLUtil.Left(arrDoc[i], 1) != "#")
                        {
                            if (arrDoc[i].Contains("/"))
                            {
                                strtemp = arrDoc[i].Substring(arrDoc[i].LastIndexOf('/') + 1);
                                strnew = "href=\"/" + ApplicationName + "/Resources/NewsImages/" + strTime + "/" + NewsID.ToString() + "/" + strtemp.Trim() + "\""; ;
                                strContent = strContent.Replace(strold, strnew);
                            }
                            else
                            {
                                strnew = "href=\"/" + ApplicationName + "/Resources/NewsImages/" + strTime + "/" + NewsID.ToString() + "/" + arrDoc[i].ToString() + "\"";
                                strContent = strContent.Replace(strold, strnew);
                            }
                        }
                    }
                }
            }
            return strContent;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="HTMLString"></param>
        /// <returns></returns>
        public static string[] GetImageScr(string HTMLString)
        {
            ArrayList arrImagesURL = new ArrayList();
            HtmlAgilityPack.HtmlDocument htmlDocs = new HtmlAgilityPack.HtmlDocument();
            htmlDocs.LoadHtml(HTMLString);
            HtmlNodeCollection NodeColl = htmlDocs.DocumentNode.SelectNodes("//img");

            if (NodeColl == null) return null;

            //Iterate through each match
            string ImageSrc = "";
            foreach (HtmlNode node in NodeColl)
            {
                ImageSrc = "";
                ImageSrc = node.Attributes["src"].Value;
                arrImagesURL.Add(ImageSrc);
            }
            //Return the list
            return (string[])arrImagesURL.ToArray(typeof(string));
        }
        public static string[] GetDocScr(string HTMLString)
        {
            ArrayList arrImagesURL = new ArrayList();
            HtmlAgilityPack.HtmlDocument htmlDocs = new HtmlAgilityPack.HtmlDocument();
            htmlDocs.LoadHtml(HTMLString);
            HtmlNodeCollection NodeColl = htmlDocs.DocumentNode.SelectNodes("//a");

            if (NodeColl == null) return null;

            //Iterate through each match
            string ImageSrc = "";
            foreach (HtmlNode node in NodeColl)
            {
               
                ImageSrc = "";
                ImageSrc = node.Attributes["href"].Value;
                if (ImageSrc.IndexOf(".doc")>0||ImageSrc.IndexOf(".txt")>0||ImageSrc.IndexOf(".pdf")>0||ImageSrc.IndexOf(".xls")>0)
                arrImagesURL.Add(ImageSrc);
            }
            //Return the list
            return (string[])arrImagesURL.ToArray(typeof(string));
        }
        #endregion
    }
}
